<template>
<div>
    <div class="box-mid">
        <el-dropdown v-hasPermission="'menu:add'" @command="handleCommand">
            <el-button type="primary">
                新增<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="0">新增菜单</el-dropdown-item>
                <el-dropdown-item command="1">新增按钮</el-dropdown-item>
            </el-dropdown-menu>
        </el-dropdown>
        <el-button  v-hasPermission="'menu:delete'" @click="handleDel">删除</el-button>
        <el-button v-hasPermission="'menu:export'" v-preventReClick="2000" @click="$emit('export')">导出Excel</el-button>
    </div>
    <div class="box-table">
        <el-table
            v-loading="tableData.loading"
            :data="tableData.rows"
            tooltip-effect="dark"
            row-key="id"
            :tree-props="{children: 'children', hasChildren: ''}"
            @selection-change="handleSelected">
            <template slot="empty">
                <no-data :types="tableData.isSearch">
                    <span>暂无数据</span>
                </no-data>
            </template>
            <el-table-column fixed type="selection" width="55"></el-table-column>
            <el-table-column fixed prop="text" label="名称" show-overflow-tooltip></el-table-column>
            <el-table-column prop="icon" label="图标" show-overflow-tooltip>
                <template slot-scope="scope"><i :class="`menuTableIcon ${scope.row.icon}`"></i></template>
            </el-table-column>
            <el-table-column
                prop="type"
                column-key="type"
                :filters="[
                    {text: '菜单', value: '0'},
                    {text: '按钮', value: '1'}
                ]"
                label="类型"
                :filter-method="filterHandler"
                show-overflow-tooltip>
                <template slot-scope="scope">
                    <el-tag
                    :type="scope.row.type === '0' ? 'primary' : 'danger'"
                    disable-transitions>{{scope.row.type==='0'?'菜单':'按钮'}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="path" label="地址" show-overflow-tooltip></el-table-column>
            <el-table-column prop="component" label="组件名称" show-overflow-tooltip></el-table-column>
            <el-table-column prop="permission" label="权限" show-overflow-tooltip></el-table-column>
            <el-table-column prop="order" label="排序" show-overflow-tooltip></el-table-column>
            <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
            <el-table-column prop="modifyTime" label="修改时间" show-overflow-tooltip></el-table-column>
            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-button v-hasPermission="'menu:update'" @click="$emit(scope.row.type === '0'?'addMenu':'addBtn', false, scope.row)" type="text">修改</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</div>
</template>

<script>
import NoData from '@/components/NoData'
export default {
    name: 'ProjectsTable',
    props: {
        tableData: {
            type: Object,
            required: true
        }
    },
    components: { NoData },
    data () {
        return {
            selected: []
        }
    },
    methods: {
        filterHandler (value, row, column) {
            const property = column['property']
            return row[property] === value
        },
        handleCommand (val) {
            this.$emit(val === '0' ? 'addMenu' : 'addBtn', true, { type: val })
        },
        handleDel () {
            if (!this.selected.length) {
                this.$message.info('请选择要删除的菜单')
            } else {
                this.$confirm('确定要删除该菜单吗?', '提示', {
                    confirmButtonText: '确 定',
                    cancelButtonText: '取 消',
                    type: 'warning'
                }).then(() => {
                    this.handleDelConfirm()
                }).catch(() => {})
            }
        },
        handleDelConfirm () {
            this.$delete('/menu', this.selected.map(v => v.id).join(',')).then(res => {
                if (res) {
                    this.$message.success('删除成功')
                    this.$emit('flash')
                }
            })
        },
        handleCurrentChange (val) {
            this.tableData.currentPage = val
            this.$emit('flash')
        },
        handleSizeChange (val) {
            this.tableData.pageSize = val
            this.$emit('flash')
        },
        handleSelected (val) {
            this.selected = val
        }
    }
}
</script>

<style lang="less">
.system-menu {
    .box-mid {
        .el-dropdown {
            margin-right: 10px;
        }
    }
    .menuTableIcon {
        font-size: 20px;
        vertical-align: middle;
    }
}
</style>
